package com.hgkj.cloud.base.scheme.table;

import com.hgkj.cloud.base.scheme.ClsAnswerRequest;
import com.hgkj.cloud.base.scheme.ClsAnswerResult;
import com.hgkj.cloud.base.scheme.ClsScore;

import java.math.BigDecimal;
import java.util.*;

/**
 * 十一、心理健康测查表（Phi）共168题
 */
public class Table11 extends AbstractTable implements TableCalculate{
    String code = "Phi";

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public ClsAnswerResult calc(ClsAnswerRequest answer) {
        return GetPhiFactorScore(answer);
    }

    @Override
    public String[] report(Map map) {
        return new String[0];
    }

    private static ClsAnswerResult GetPhiFactorScore(ClsAnswerRequest answer) {
        ClsAnswerResult result = new ClsAnswerResult();
        result.setMessage("");
        result.setType(answer.getType());
        result.setUuid(answer.getUuid());
        try {
            // 1正确题数
            int MAX_NUM = 168;
            // '1躯体化Som，2抑郁Dep，3焦虑Anx，4病态人格Psd，5疑心Hyp，6脱离现实Unr，7兴奋状态Hma，8弃权的题数Q，9说谎分L，10诈病或装坏F
            int intI;
            String[] strAnswer_c = answer.getAnswer().split(g_sChar);
            if (strAnswer_c.length > 1) {
                for (intI = 1; intI < strAnswer_c.length; intI++) {
                    strAnswer_c[intI] = strAnswer_c[intI].trim();
                }
            }

            String[] sAnswer = strAnswer_c[0].toString().split(g_nChar);

            if (sAnswer.length != MAX_NUM) {
                result.setMessage("答案数量不符！");
                return result;
            }

            int i, j;

            // 8 弃权的题数Q，全部题目，选择3得1分
            // 题目分组，共2组，第一组0,1，第二组1,0，共8个因子
            String[] strNumberOneA = { "1,8,24,26,33,47,73,74,79,82,83,93,94,95,113,119",
                    "3,17,21,40,60,65,85,91,103,118,124,125,133,135,150", "4,51,80", "3,45", "43,85,98", "98", "", "",
                    "13,28,36,42,43,49,58,104", "74,84,94,95,110,125" };
            String[] strNumberTwoA = { "10,14,20,30,34,50,52,56,63,78,122", "19,32,37,39,48,69,76,88,92,105,114",
                    "2,14,68,86,90,109,115,138,142,144,145,147,148,154,155,157,158,159,160,163",
                    "7,9,15,16,23,27,29,38,44,46,53,55,57,70,102,108,112,116,120,121,130,139",
                    "7,11,14,54,59,62,64,67,75,97,99,127,128,130,132,137,145,151,161,162,164",
                    "6,18,25,31,61,72,77,89,117,127,129,134,136,140,141,143,146,149,152,153,156",
                    "5,9,12,35,53,60,74,81,87,96,101,107,111,123,131,146,165,166,167,168", "", "",
                    "10,22,23,27,38,61,70,72,89,97,100,102,105,106,122,126,160" };
            String[] strSubA;
            float[][] sngStandardA;

            // 诊断结果
            String[] vAppraiseA = new String[10];
            // 指导意见，多一个“总结”
            String[] vDoctorIdeaA = new String[11];

            for (i = 0; i < vAppraiseA.length; i++) {
                vAppraiseA[i] = "";
            }

            for (i = 0; i < vDoctorIdeaA.length; i++) {
                vDoctorIdeaA[i] = "";
            }

            float[][] vFactorA = new float[10][2]; // 实际为10个因子
            for (i = 0; i < vFactorA.length; i++) {
                for (j = 0; j < vFactorA[1].length; j++) {
                    vFactorA[i][j] = 0;
                }
            }

            // 原始分
            // 第一组
            for (i = 0; i < strNumberOneA.length; i++) {
                if (strNumberOneA[i].length() > 0) {
                    strSubA = strNumberOneA[i].split(",");
                    for (j = 0; j < strSubA.length; j++) {
                        if (Integer.valueOf(sAnswer[Integer.valueOf(strSubA[j]) - 1]) == 2) {
                            vFactorA[i][0] += 1;
                        }
                    }
                }
            }

            // 第二组
            for (i = 0; i < strNumberTwoA.length; i++) {
                if (strNumberTwoA[i].length() > 0) {
                    strSubA = strNumberTwoA[i].split(",");
                    for (j = 0; j < strSubA.length; j++) {
                        if (Integer.valueOf(sAnswer[Integer.valueOf(strSubA[j]) - 1]) == 1) {
                            vFactorA[i][0] += 1;
                        }
                    }
                }
            }

            // 弃权题数
            for (i = 0; i < sAnswer.length; i++) {
                if (Integer.valueOf(sAnswer[i]) == 3) {
                    vFactorA[7][0] += 1;
                }
            }

            // 标准分
            // 公式：Cint(50 + 10 * (Original(i) – arrMinus(i)) / arrDivisor(i))
            int intSex = answer.getSex();
            if (intSex == 1) {
                float[][] sngValueA = { { 6.56f, 8.9f, 7.4f, 6.76f, 6.89f, 6.02f, 6.73f, 3.78f, 2.81f, 2.76f },
                        { 4.02f, 3.68f, 4.44f, 3.6f, 3.54f, 3.72f, 3.7f, 5.24f, 1.68f, 2.13f } };
                sngStandardA = sngValueA;
            } else {
                float[][] sngValueA = { { 7.37f, 9.83f, 8.07f, 6.23f, 7.23f, 6.41f, 6.67f, 3.82f, 2.73f, 2.65f },
                        { 4.08f, 3.84f, 4.44f, 3.4f, 3.61f, 3.8f, 3.65f, 5.06f, 1.66f, 1.93f } };
                sngStandardA = sngValueA;
            }
            for (i = 0; i < vFactorA.length; i++) {
                /*
                 * vFactorA[i][1] = (int) (50 + 10 (vFactorA[i][0] - sngStandardA[0][i]) /
                 * sngStandardA[1][i]);
                 */
                BigDecimal bd = new BigDecimal((50 + 10 * (vFactorA[i][0] - sngStandardA[0][i]) / sngStandardA[1][i]));
                vFactorA[i][1] = bd.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
            }

            // 按标准分得到“诊断结果”
            for (i = 0; i < vFactorA.length; i++) {
                if (vFactorA[i][1] >= 70) {
                    vAppraiseA[i] = "**";
                } else if (vFactorA[i][1] >= 60) {
                    vAppraiseA[i] = "*";
                }
            }

            // 按标准分得到“指导意见”
            // 第一部分
            int[][] intGradeA = { { 81, 71, 61, 41, 0 }, { 70, 60, 51, 0, 0 }, { 70, 60, 51, 0, 0 },
                    { 70, 60, 51, 0, 0 }, { 70, 60, 51, 0, 0 }, { 70, 60, 51, 0, 0 }, { 70, 60, 51, 0, 0 },
                    { 63, 51, 0, 0, 0 }, { 71, 61, 0, 0, 0 }, { 90, 71, 61, 0, 0 } };
            for (i = 0; i < intGradeA.length; i++) {
                for (j = 0; j < intGradeA[1].length; j++) {
                    if (vFactorA[i][1] >= intGradeA[i][j]) {
                        vDoctorIdeaA[i] = "Phi." + String.valueOf(i + 1) + "." + String.valueOf(j + 1);
                        // System.out.println(vDoctorIdeaA[i]);
                        break;
                    }
                }
            }

            // 第二部分
            // 从大到小进行排序，按标准分
            float[] intValue = new float[7];
            int[] intSortA = { 1, 2, 3, 4, 5, 6, 7 };
            String strSign = "";
            for (i = 0; i < intValue.length; i++) {
                intValue[i] = vFactorA[i][1];
                if (intValue[i] >= 60) {
                    intValue[i] += (6 - i) / 100; // 2006-07-19 防止分数相同
                }
            }

            // Array.Sort(intValue, intSortA) //排序，但从小到大
            Map<Float, Integer> map = new TreeMap<Float, Integer>(new Comparator<Float>() {
                public int compare(Float obj1, Float obj2) {
                    // 升序排序
                    return obj1.compareTo(obj2);
                }
            });
            for (i = 0; i < intValue.length; i++) {
                map.put(intValue[i], intSortA[i]);
                // System.out.println(intValue[i] + ":" + intSortA[i]);
            }

            Set<Float> keySet = map.keySet();
            Iterator<Float> iter = keySet.iterator();
            i = 0;
            while (iter.hasNext()) {
                float key = iter.next();
                intValue[i] = key;
                intSortA[i] = map.get(key);
                // System.out.println(intValue[i] + ":" + intSortA[i]);
                i += 1;
            }

            int[] intFileA = { 12, 13, 14, 15, 16, 17, 23, 24, 25, 26, 27, 34, 35, 36, 37, 45, 46, 47, 56, 57, 67 };

            if (intValue[5] >= 60 && intValue[6] >= 60) // 2006-06-21
            {
                if (intSortA[5] > intSortA[6]) {
                    strSign = String.valueOf(intSortA[6]) + String.valueOf(intSortA[5]);
                } else {
                    strSign = String.valueOf(intSortA[5]) + String.valueOf(intSortA[6]);
                }
                for (i = 0; i < intFileA.length; i++) {
                    if (Integer.valueOf(strSign) == intFileA[i]) {
                        vDoctorIdeaA[10] = "Phi.11." + String.valueOf(i + 1);
                        break;
                    }
                }
                vDoctorIdeaA[intSortA[5] - 1] = "";
                vDoctorIdeaA[intSortA[6] - 1] = "";
            }

            ClsScore.Phi phi = new ClsScore.Phi();
            phi.setSom_o(vFactorA[0][0]);
            phi.setSom_s(vFactorA[0][1]);
            phi.setSom_a(vAppraiseA[0]);
            phi.setSom_d(vDoctorIdeaA[0]);

            phi.setDep_o(vFactorA[1][0]);
            phi.setDep_s(vFactorA[1][1]);
            phi.setDep_a(vAppraiseA[1]);
            phi.setDep_d(vDoctorIdeaA[1]);

            phi.setAnx_o(vFactorA[2][0]);
            phi.setAnx_s(vFactorA[2][1]);
            phi.setAnx_a(vAppraiseA[2]);
            phi.setAnx_d(vDoctorIdeaA[2]);

            phi.setPsd_o(vFactorA[3][0]);
            phi.setPsd_s(vFactorA[3][1]);
            phi.setPsd_a(vAppraiseA[3]);
            phi.setPsd_d(vDoctorIdeaA[3]);

            phi.setHyp_o(vFactorA[4][0]);
            phi.setHyp_s(vFactorA[4][1]);
            phi.setHyp_a(vAppraiseA[4]);
            phi.setHyp_d(vDoctorIdeaA[4]);

            phi.setUnr_o(vFactorA[5][0]);
            phi.setUnr_s(vFactorA[5][1]);
            phi.setUnr_a(vAppraiseA[5]);
            phi.setUnr_d(vDoctorIdeaA[5]);

            phi.setHma_o(vFactorA[6][0]);
            phi.setHma_s(vFactorA[6][1]);
            phi.setHma_a(vAppraiseA[6]);
            phi.setHma_d(vDoctorIdeaA[6]);

            phi.setQ_o(vFactorA[7][0]);
            phi.setQ_s(vFactorA[7][1]);
            phi.setQ_a(vAppraiseA[7]);
            phi.setQ_d(vDoctorIdeaA[7]);

            phi.setLie_o(vFactorA[8][0]);
            phi.setLie_s(vFactorA[8][1]);
            phi.setLie_a(vAppraiseA[8]);
            phi.setLie_d(vDoctorIdeaA[8]);

            phi.setF_o(vFactorA[9][0]);
            phi.setF_s(vFactorA[9][1]);
            phi.setF_a(vAppraiseA[9]);
            phi.setF_d(vDoctorIdeaA[9]);

            phi.setTotal_d(vDoctorIdeaA[10]);

            result.setObject(phi);
            return result;
        } catch (ArithmeticException e) {
            result.setMessage(e.toString());
            return result;
        }
    }


}
